﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
using System.Threading;

namespace CrunchbaseGrabber
{
    class Program
    {
        static void Main(string[] args)
        {
            CrunchJsonStream cjStream = new CrunchJsonStream();
            CompanyGenerator companyGenerator = new CompanyGenerator();
            JavaScriptSerializer ser = new JavaScriptSerializer();
            StreamWriter file = new StreamWriter("pivot.txt");

            string jsonStream;

            //write the column headers
            file.WriteLine("\tCompany Logo URL\tCompany Name\tHomepage URL\tCrunchbase URL\tShort Description\tCategory\tNumber of Employees\tCity\tState\tCountry\tYear Founded\tTotal Funding\tKeyword1\tKeyword2\tKeyword3\tKeyword4\tKeyword5"); //\tOverview");
            
            Console.WriteLine("Getting ready to get the company names");
            
            //go hit the crunchbase api to get a list of all the companies in their database
            List<cbCompanyObject> companyNames = companyGenerator.GetCompanyNames();
            Console.WriteLine("Back from getting the company names");

            foreach (cbCompanyObject company in companyNames)
            {
                string jsonLine;

                //with a company name parsed from JSON, create the stream of the company specific JSON
                jsonStream = cjStream.GetJsonStream(company.name);

                if (jsonStream != null)
                {
                    try
                    {
                        //with the stream, now deserialize into the Crunchbase object
                        CrunchBase jsonCrunchBase = ser.Deserialize<CrunchBase>(jsonStream);

                        //assuming that worked, we need to clean up and create some additional meta data
                        jsonCrunchBase.FixCrunchBaseURL();
                        jsonCrunchBase.AggregateFunding();
                        jsonCrunchBase.SplitTagString();

                        //and now we build the CSV string and write to file
                        jsonLine = "\t" + jsonCrunchBase.GetImageURL() + "\t" +
                            jsonCrunchBase.name + "\t" +
                            jsonCrunchBase.homepage_url + "\t" +
                            jsonCrunchBase.crunchbase_url + "\t" +
                            jsonCrunchBase.description + "\t" +
                            jsonCrunchBase.category_code + "\t" +
                            jsonCrunchBase.number_of_employees + "\t" +
                            jsonCrunchBase.offices[0].city + "\t" +
                            jsonCrunchBase.offices[0].state_code + "\t" +
                            jsonCrunchBase.offices[0].country_code + "\t" +
                            jsonCrunchBase.founded_year + "\t" +
                            jsonCrunchBase.GetAggregateFunding().ToString() + "\t" +
                            jsonCrunchBase.GetKeywordList(); /* + "\t\"" +  //there are max of 5 keywords I will dump out
                            jsonCrunchBase.overview + "\"";*/
                            //I really wanted to put the "overview" field into the file, but the XLS import was
                            //blowing up on the HTML...will need to revisit and fix

                        file.WriteLine(jsonLine);
                        Console.WriteLine("Finished with {0}", jsonCrunchBase.name);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Oops, the exception {0} happened with {1}",e.ToString(), company.name);
                    }
                }
                
                //I had to put this in because without it, after about 900 pings, the crunch base database appeared
                //to be locking me out.  I put this in to be nice to the server, but wow did things slow down
                Thread.Sleep(500);
            }            
            file.Close();
            Console.WriteLine("done");
        }
    }

    public class CompanyGenerator
    {
        //this is how we call out to crunchbase to get their full list of companies
        public List<cbCompanyObject> GetCompanyNames()
        {
            string jsonStream;
            JavaScriptSerializer ser = new JavaScriptSerializer();

            WebRequest wrGetURL;
            wrGetURL = WebRequest.Create("http://api.crunchbase.com/v/1/companies.js");

            jsonStream = new StreamReader(wrGetURL.GetResponse().GetResponseStream()).ReadToEnd();

            //as opposed to the single company calls, this returns a list of companies, so we have to
            //stick it into a list
            List<cbCompanyObject> jsonCompanies = ser.Deserialize<List<cbCompanyObject>>(jsonStream);

            return jsonCompanies;
        }
        
    }

    public class CompanyJsonObject
    {
        public List<cbCompanyObject> cbcompanyObj;
    }

    //the simple definition for the deserialized object
    public class cbCompanyObject
    {
        public string name;
        public string permalink;    
    }
}
